package com.jason.apps.buyer;

import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.apps.essentials.ConsumeWebService;
import com.apps.realtyportal.LoginActivity;
import com.apps.realtyportal.R;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

public class Favorites extends SherlockFragment{

	    OnClickSelectedListener mCallback;

		BuyerAdapter adapter;
		
		ConsumeWebService c;
		
		public final static ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
		
		HashMap<String, String> map;
		
		private ListView lv;
		
		SherlockFragmentActivity fa;
		
		public static final String KEY_ID = "property_id";
		public static final String KEY_NAME = "property_name";
		public static final String KEY_DESCRIPTION = "property_description";
		public static final String KEY_PRICE = "price";
		public static final String KEY_LATITUDE = "latitude";
		public static final String KEY_LONGITUDE = "longitude";
		public static final String KEY_ADDRESS = "address";
		public static final String KEY_TYPE = "type";
		public static final String KEY_IMAGE = "main_image";
		public static final String KEY_PERSONFNAME="firstname";
		public static final String KEY_PERSONLNAME="lastname";
		public static final String KEY_PERSONCONTACT="mobile";
		
		private ListView list;
		public static int property_id=-1;
			
		public void displayList(){
			
			c = new ConsumeWebService();
			
			 String data = c.getLeadProperty(LoginActivity.id);
			 
			 try {

					JSONArray array = new JSONArray(data);
					
					for (int i = 0; i < array.length(); i++) {
						map = new HashMap<String, String>();
						JSONObject e = array.getJSONObject(i);
						map.put("property_id", e.getString(KEY_ID));
						map.put("property_name", e.getString(KEY_NAME));
						map.put("property_description", e.getString(KEY_DESCRIPTION));
						map.put("price", e.getString(KEY_PRICE));
						map.put("latitude", e.getString(KEY_LATITUDE));
						map.put("longitude", e.getString(KEY_LONGITUDE));
						map.put("address", e.getString(KEY_ADDRESS));
						map.put("type", e.getString(KEY_TYPE));
						map.put("image", e.getString(KEY_IMAGE));
						map.put("firstname", e.getString("firstname"));
						map.put("lastname", e.getString("lastname"));
						map.put("mobile", e.getString("mobile"));
						map.put("email", e.getString("email_address"));
						map.put("status", e.getString("status"));
						map.put("contacts", e.getString("mobile")+"/"+ e.getString("landline"));
						
						mylist.add(map);
					}
				} catch (JSONException e) {
					Log.e("log_tag", "Error parsing data " + e.toString()+"diri jud");
				}finally{
					
				}
			 }
		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
			View view=inflater.inflate(R.layout.listforeditproperty, container, false);
			fa=(SherlockFragmentActivity)super.getActivity();
			mylist.clear();
			
			adapter=new BuyerAdapter(getActivity(),mylist);			
			lv=(ListView)view.findViewById(R.id.list_editproperty);
			new Load().execute();
			
			
			
			return view;
	    }
		 @Override
		    public void onAttach(Activity activity) {
		        super.onAttach(activity);
		        
		        try {
		            mCallback = (OnClickSelectedListener) activity;
		        } catch (ClassCastException e) {
		            throw new ClassCastException(activity.toString()
		                    + " must implement OnHeadlineSelectedListener");
		        }
		    }

		
	    public interface OnClickSelectedListener {
	        /** Called by HeadlinesFragment when a list item is selected */
	        public void onArticleSelected(View v,int position);
	    }
	    
	    public class Load extends AsyncTask<Void, Void, Void> {

		    ProgressDialog dialog;

		  	    protected void onPreExecute() {
		  	    	dialog = new ProgressDialog(fa);
		  	            dialog.setMessage("Loading...");
		  	            dialog.show();
		  	    }
			    protected Void doInBackground(final Void... unused) {
			    	
					try {
						displayList();	
				
					} catch (Exception e) {
						Log.e("","wala jud ma add",e);
					}
			   
			        return null; 
			    }
			    protected void onPostExecute(Void result) {
			    	
					lv.setAdapter(adapter);
					
					if (dialog!=null) {
			            dialog.dismiss();
			            }  
			     }
			  }	    
	    
	    
	}